<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * create an entry for a module item - hook for ('item','create','GUI')
 * Optional $extrainfo['keywords'] from arguments, or 'keywords' from input
 *
 * @author mikespub
 * @param $args['objectid'] ID of the object
 * @param $args['extrainfo'] extra information
 * @returns array
 * @return extrainfo array
 * @throws BAD_PARAM, NO_PERMISSION, DATABASE_ERROR
 */
function conversations_userapi_createusers($args)
{
    extract($args);

    if (!isset($objectid) || !is_numeric($objectid)) {
        $msg = xarML('Invalid #(1) for #(2) function #(3)() in module #(4)', 'object ID', 'admin', 'createhook', 'conversations');
        xarErrorSet(XAR_USER_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }
    if (!isset($extrainfo) || !is_array($extrainfo)) {
        $extrainfo = array();
    }

    // When called via hooks, modname wil be empty, but we get it from the
    // extrainfo or the current module
    if (empty($modname)) {
        if (!empty($extrainfo['module'])) {
            $modname = $extrainfo['module'];
        } else {
            $modname = xarModGetName();
        }
    }
    $modid = xarModGetIDFromName($modname);
    if (empty($modid)) {
        $msg = xarML('Invalid #(1) for #(2) function #(3)() in module #(4)', 'module name', 'admin', 'createhook', 'keywords');
        xarErrorSet(XAR_USER_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }

    if (!isset($itemtype) || !is_numeric($itemtype)) {
         if (isset($extrainfo['itemtype']) && is_numeric($extrainfo['itemtype'])) {
             $itemtype = $extrainfo['itemtype'];
         } else {
             $itemtype = 0;
         }
    }

    // check if we need to save some keywords here
    if (isset($extrainfo['itemid']) && is_numeric($extrainfo['itemid'])) {
        $uid = $extrainfo['itemid'];
    } else {
        return $extrainfo;
    }

    //Let's set defaults so we can pass in values if necessary
    if ((!isset($tagcount)) || (empty($tagcount))){
        $tagcount = 0;
    }
    if ((!isset($postcount))|| (empty($postcount))){
        $postcount = 0;
    }
    if ((!isset($options))|| (empty($options))){
        $options = serialize(null);
    }
    if ((!isset($timestamp))|| (empty($timestamp))){
        $timestamp = time();
    }
    
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['cusers'];
    // Add item
    $query = "INSERT INTO $table ("
        . " xar_uid, xar_tagcount, xar_postcount, "
        . " xar_timestamp, xar_options)"
        . " VALUES (?, ?, ?, ?, ?)";
    $result =& $dbconn->Execute($query, array($uid, $tagcount, $postcount, $timestamp, $options));
    if (!$result) return;
    // Return the id of the newly created link to the calling process
    return $extrainfo;
}
?>